$(document).ready(function(){



    $('.log_form').bind('submit',function(e){
        //do your test
        //if fails
       // alert('1');
        console.log('begin');
        var user = $('.user_name').val();
        //alert(user);
        var pass = $('.user_pass').val();
        if(user===''){
            console.log('empty user');
            $('.wrong_data').text('Ви не ввели імя користуввача');
            $('.wrong_data').show();
            e.preventDefault();
            return;
        }
        if(pass==''){
            console.log('empty pass');
            $('.wrong_data').text('Ви не ввели пароль користуввача');
            $('.wrong_data').show();
            e.preventDefault();
            return;
        }

        console.log('end');


    });

    $('.menu_item').on({

        mouseenter: function() {
            console.log('in');
            $(this).css('background-color','#939393');
            $(this).css('color','#ffffff');
        },
        mouseleave: function() {
            $(this).css('background-color','#ffffff');
            $(this).css('color','#555555');
        }
    });



        var options = {
            target:   '#output',   // target element(s) to be updated with server response
            beforeSubmit:  beforeSubmit,  // pre-submit callback
            success:       afterSuccess,  // post-submit callback
            uploadProgress: OnProgress, //upload progress callback
            resetForm: true        // reset the form after successful submit
        };


    $('#mid_file_upload_form').submit( function( e ) {
        $.ajax( {
            url: 'http://door-market/php/processupload.php',
            type: 'POST',
            data: new FormData( this ),
            processData: false,
            contentType: false,
            success: function (data) {

                var file_name = data.match(/[\d]+.jpg/);
                file_name= file_name[0];
                //alert(file_name);
                var src = '../file_upload/uploads/' + file_name;
                $('.mid_img').attr("src", src);
                //alert(data);
                return false;
            }
        } );
        e.preventDefault();
    } );

    $('#lar_file_upload_form').submit( function( e ) {
        $.ajax( {
            url: 'http://door-market/php/processupload.php',
            type: 'POST',
            data: new FormData( this ),
            processData: false,
            contentType: false,
            success: function (data) {

                var file_name = data.match(/[\d]+.jpg/);
                file_name= file_name[0];
                //alert(file_name);
                var src = 'http://door-market/file_upload/uploads/' + file_name;
                $('.lar_img').attr("href", src);
                //alert(data);
                return false;
            }
        } );
        e.preventDefault();
    } );

    $('#sm_file_upload_form').submit( function( e ) {
        console.log("begin")
        $.ajax( {
            url: 'http://door-market/php/processupload.php',
            type: 'POST',
            data: new FormData( this ),
            processData: false,
            contentType: false,
            success: function (data) {

                var file_name = data.match(/[\d]+.jpg/);
                file_name= file_name[0];
                //alert(file_name);
                var src = 'http://door-market/file_upload/uploads/' + file_name;
                $('.sm_img').attr("src", src);
                //alert(data);
                return false;
            }
        } );
        e.preventDefault();
    } );



    $('.save_button').on('click',function(e){
        //alert(1);
        console.log('save_btn');
        e.preventDefault();


        var product_type = $( ".product_type_select" ).val();
        var product_model = $( ".product_model_input" ).val();
        var product_material = $( ".product_material_select" ).val();
        var product_color = $( ".product_color_select" ).val();


        var mid_image =$(".mid_img").attr('src');
        var lar_image =$(".lar_img_im").attr('src');
        var sm_image =$(".sm_img").attr('src');


        var undef_mid_image = 'photo_doesetn_upload_mig.jpg';
        var undef_lar_image = 'photo_doesetn_upload_lar.jpg';
        var undef_sm_image = 'photo_doesetn_upload_sm.jpg';

        var mid_image_is_correct = true;
        var lar_image_is_correct = true;
        var sm_image_is_correct = true;

        var undef_select_val = 'undef';

        if(product_type==undef_select_val ){
            $.pgwModal({
                content: 'Ви не вказали тип товару',
                title: 'Помилка',
                maxWidth: 400
            });
            return;
        }

        if(product_model=='' ){
            $.pgwModal({
                content: 'Ви не вказали модель товару',
                title: 'Помилка',
                maxWidth: 400
            });
            return;
        }

        if(product_material==undef_select_val ){
            $.pgwModal({
                content: 'Ви не вказали матеріал товару',
                title: 'Помилка',
                maxWidth: 400
            });
            return;
        }

        if(product_material==undef_select_val ){
            $.pgwModal({
                content: 'Ви не вказали колір товару',
                title: 'Помилка',
                maxWidth: 400
            });
            return;
        }

        if (mid_image.indexOf(undef_mid_image) >= 0){
           // alert(1);
            mid_image_is_correct = false;
            //console.log('ttt');
            $.pgwModal({
                content: 'Ви не вибрали середнє зображення',
                title: 'Помилка',
                maxWidth: 400
            });
            return;
        }

        console.log(lar_image);
        if (lar_image.indexOf(undef_lar_image) >= 0){
            lar_image_is_correct = false;
            $.pgwModal({
                content: 'Ви не велике середнє зображення',
                title: 'Помилка',
                maxWidth: 400
            });
            return;
        }
        if (sm_image.indexOf(undef_sm_image) >= 0){
            sm_image_is_correct = false;
            $.pgwModal({
                content: 'Ви не маленьке середнє зображення',
                title: 'Помилка',
                maxWidth: 400
            });
            return;
        }


        $.pgwModal({
            content: 'Товар успішно збережено',
            title: 'Ок',
            maxWidth: 400
        });

        setTimeout(function(){
            $.pgwModal('close');
        }, 1500);

        console.log(product_type);
        console.log(product_model);
        console.log(product_material);
        console.log(product_color);

        console.log(mid_image);

    });


});

function show_modal(){
    console.log('begin');
    $.pgwModal({
        target: '#modalContent',
        title: 'Modal title 2',
        maxWidth: 1100
    });
};
function upload_mid_photo(){
    console.log('upload_mid_photo begin');
    $(".hidden").trigger('click');
    return false;
};



function beforeSubmit(){
    //check whether client browser fully supports all File API
    if (window.File && window.FileReader && window.FileList && window.Blob)
    {
        var fsize = $('#FileInput')[0].files[0].size; //get file size
        var ftype = $('#FileInput')[0].files[0].type; // get file type
        //allow file types
        switch(ftype)
        {
            case 'image/png':
            case 'image/gif':
            case 'image/jpeg':
            case 'image/pjpeg':
            case 'text/plain':
            case 'text/html':
            case 'application/x-zip-compressed':
            case 'application/pdf':
            case 'application/msword':
            case 'application/vnd.ms-excel':
            case 'video/mp4':
                break;
            default:
                $("#output").html("<b>"+ftype+"</b> Unsupported file type!");
                return false
        }

        //Allowed file size is less than 5 MB (1048576 = 1 mb)
        if(fsize>5242880)
        {
            alert("<b>"+fsize +"</b> Too big file! <br />File is too big, it should be less than 5 MB.");
            return false
        }
    }
    else
    {
        //Error for older unsupported browsers that doesn't support HTML5 File API
        alert("Please upgrade your browser, because your current browser lacks some new features we need!");
        return false
    }
};

function OnProgress(event, position, total, percentComplete)
{
    //Progress bar
    $('#progressbox').show();
    $('#progressbar').width(percentComplete + '%') //update progressbar percent complete
    $('#statustxt').html(percentComplete + '%'); //update status text
    if(percentComplete>50)
    {
        $('#statustxt').css('color','#000'); //change status text to white after 50%
    }
};

function afterSuccess(){};







